package com.xiaojimao.Interceptor;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.xaiojimao.gr_common.CommonPojo.OpenUser;
import com.xaiojimao.gr_common.Utils.JwtUtils;
import com.xiaojimao.Utils.ThreadLocalUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ArticleInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        if (StringUtils.isEmpty(token) || !JwtUtils.isTokenValid(token)) {
            response.setStatus(401);
            return false;
        }

        OpenUser article = new OpenUser();
        Long id = Long.valueOf(JwtUtils.getTokenInfo(token));
        article.setId(id);
        ThreadLocalUtil.setUser(article);
        return true;
    }
}
